package hp.com.controller;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.fabric.xmlrpc.base.Data;

import hp.com.entity.Car;
import hp.com.entity.Manager;
import hp.com.entity.Order;
import hp.com.entity.User;
import hp.com.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService service;
	//用户登陆
	@RequestMapping("/userdeng")
	public String login(User user,HttpSession session){
		User u = service.login(user);
		if(u!=null){
			session.setAttribute("unamename", u.getUnamename());
			return "index2";
		}else{
			session.setAttribute("msg", "账号或密码错误");
			return "redirect:/login.jsp";
		}
	}
	//用户注册
	@RequestMapping("/userzhu")
	public String zhuce(User user,HttpSession session){
		//注册前的全查
		User u = service.selectByName(user);
		if(u!=null){
			//注册
			session.setAttribute("msg", "用户名已存在，请重新输入");
			return "redirect:/login.jsp";
		}else{
			int a = service.zhuce(user);
			if(a>0){
				session.setAttribute("msg", "注册成功，请重新登陆");
				return "redirect:/login.jsp";
			}else{
				session.setAttribute("msg", "注册失败");
				return "redirect:/login.jsp";
			}
			
		}
	} 
	//个人信息管理
	@RequestMapping("/select")
	@ResponseBody
	public User select(HttpSession session){
		String unamename =(String) session.getAttribute("unamename");
		User user=new User();
		user.setUnamename(unamename);
		User u = service.selectByName(user);
		return u;
	}
	//个人信息
	@RequestMapping("/select1")
	public String selct1(){
		return "redirect:/geren.jsp";
	}
	//个人信息完善
	@RequestMapping("/yupdate")
	@ResponseBody
	public int yupdate(User user,HttpSession session){
		System.out.println("修改"+user);
		int a = service.yupdate(user);
		return a;
	}
	//个人租车订单
	@RequestMapping("/dingdan")
	@ResponseBody
	public PageInfo dingdan(@RequestParam(value = "pn", defaultValue = "1") Integer pn,HttpSession session){
		PageHelper.startPage(pn,7);
		String unamename =(String) session.getAttribute("unamename");
		User user=new User();
		user.setUnamename(unamename);
		List<Order> list = service.dingdan(user);
		PageInfo page = new PageInfo(list,7);
		return page;
	}
	//汽车租赁
	@RequestMapping("/zulin")
	@ResponseBody
	public PageInfo zulin(@RequestParam(value = "pn", defaultValue = "1") Integer pn,HttpSession session){
		//汽车租赁中的判断用户的信息是否完善
		String msg=null;
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		if(p.getUphone()==null || p.getUsex()==null || p.getUcard()==null || p.getUphone().length()==0 ||p.getUcard().length()==0||p.getUsex().length()==0 ){
			msg="no";
		}else{
			msg="ok";		
		}
		//分页
		PageHelper.startPage(pn,7);
		List<Car> list = service.zulin();
		for (Car car : list) {
			car.setMsg1(msg);
		}
		PageInfo page = new PageInfo(list,7);
		return page;
	}
	//租赁汽车的回显
	@RequestMapping("/zuselectbyid")
	@ResponseBody
	public Car zuselectbyid(Integer cid){
		Car c = service.zuselectbyid(cid);
		return c;
	}
	//租赁汽车订单的添加
	@RequestMapping("/insertDing")
	@ResponseBody
	public String insertDing(Order order,HttpSession session){
		String msg=null;
		//获取当前用户对象
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		//判断用户是否只租赁了一辆车
		List<Order> list = service.zushu(p);
		if(list.size()>0){
			msg="0";
			return msg;
		}else{
			//获取用户账户余额
			double um=p.getUamount();
			//获取用于租车的时间和车辆的收费标准，计算费用
			int otime =order.getOtime();
			double cmoney = order.getCmoney();
			double otime2 =(otime*cmoney);
			//判断账户余额是否足够租车
			if(um>otime2){
				//租车成功后修改用户的账户余额
				p.setUamount(um-otime2);
				int a = service.updateUser(p);
				//对订单的对象赋值
				Date date =new Date();
				order.setUid(p.getUid());
				order.setOmoney(otime2);
				//getTime()返回距 1970 年 1 月 1 日之间的毫秒数：以后有用
				order.setZhm(date.getTime());
				service.insertDing(order);
				//修改汽车状态
				Car cc=new Car();
				cc.setCid(order.getCid());
				cc.setCstate("已出租");
				int b = service.updatecar(cc);
				msg="1";
				return msg;
			}else{
				msg="2";
				return msg;
			}
		}
	}
	//还车前判端用户是否有，未归还的车辆订单
	@RequestMapping("/selectding")
	@ResponseBody
	public String selectding(HttpSession session){
		String msg=null;
		//获取当前用户对象
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		//判断用户是否只租赁了一辆车
		List<Order> list = service.zushu(p);
		if(list.size()>0){
			msg="1";
			return msg;
		}else{
			msg="0";
			return msg;
		}
	}
	//还车通过用户id查询出用户租车订单
	@RequestMapping("/selecthuanche")
	@ResponseBody
	public List<Order> selecthuanche(HttpSession session){
		//获取当前用户对象
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		//查出当前用户未还车的订单
		List<Order> list = service.selecthuanche(p);
		return list;
	}
	
	//通过id还车
	@RequestMapping("/huanche")
	@ResponseBody
	public String huanche(HttpSession session){
		//获取当前用户对象
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		//查出当前用户未还车的订单
		List<Order> list = service.selecthuanche(p);
		Order o=list.get(0);
		//修改汽车状态
		Car cc=new Car();
		cc.setCid(o.getCid());
		cc.setCstate("空闲");
		int b = service.updatecar(cc);
		//获得租车时刻，距 1970 年 1 月 1 日之间的毫秒数
		long zotime=o.getZhm();
		//获得还车时刻，距 1970 年 1 月 1 日之间的毫秒数
		Date date=new Date();
		long zotime2=date.getTime();
		int a=Math.round((zotime2-zotime)/(1000*60*60*24));	
		/*一天有86400000即(1000*60*60*24)毫秒    
		 * o.getOtime()表示约定时间 比如是 3
		 * （zotime2-zotime）表示 实际租车时间
		 * 
		 * 如果租车时间（zotime2-zotime）正好为86400000，
		 * 那么(zotime2-zotime)/(1000*60*60*24)=1<3 
		 * 即用户在约定时间内还车（但这这种情况概率太低，精确的毫秒，可以忽略） 
		 * 
		 * 如果租车时间（zotime2-zotime）小于86400000，
		 * 那么(zotime2-zotime)/(1000*60*60*24)=0<3
		 *  即用户在约定时间内还车 ,并且该用户租车时间没有一天
		 * 
		 * 如果租车时间（zotime2-zotime）大于86400000，但小于86400000*2，
		 * 那么(zotime2-zotime)/(1000*60*60*24)=1<3
		 * 即用户在约定时间内还车,并且该用户租车时间大于一天,小于两天
		 * 
		 * .......
		 * 
		 * 如果租车时间（zotime2-zotime）大于86400000*3，但小于86400000*4，
		 * 那么(zotime2-zotime)/(1000*60*60*24)=3=3
		 * 即用户在约定时间外还车，大于三天,小于四天。除去用户租车的时间刚好等于86400000*3的情况，
		 * */
		//a小于约定时间，即用户在约定时间内还车
		if(a<o.getOtime()){
			o.setZotime(a+1);
			o.setZomoney(o.getOmoney());
			service.updateDingdan(o);
			return "0";
		}else{
			//用户在约定时间外还车
			System.out.println("bbbbbbbbbbbbbbbbbb");
			o.setZotime(a+1);
			o.setZomoney(o.getOmoney()+((a+1)-o.getOtime())*o.getCmoney()+30);
			//修改订单
			service.updateDingdan(o);
			//用户超时，将超时的费用从用户的账户中扣除
			p.getUamount();
			User user2=new User();
			user2.setUamount(p.getUamount()-(((a+1)-o.getOtime())*o.getCmoney()+30));
			user2.setUnamename(p.getUnamename());
			//对用户余额修改，扣除用户超时的费用和惩罚金
			service.chongzhi(user2);
			
			return "1";
		}
	}
	//充值前判断用户的个人信息是否完善
	@RequestMapping("/chongzhihui")
	@ResponseBody
	public String chongzhihui(HttpSession session){
		//获取当前用户对象
		String msg=null;
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		if( p.getUphone()==null || p.getUsex()==null || p.getUcard()==null ||p.getUphone().length()==0 ||p.getUcard().length()==0||p.getUsex().length()==0 ){
			msg="0";
			return msg;
		}else{
			msg="1";
			return msg;
		}
	}
	//充值前回显个人信息
	@RequestMapping("/chongzhihui2")
	@ResponseBody
	public User chongzhihui2(HttpSession session){
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);	
		return p;
	}
	//充值金额
	@RequestMapping("/chongzhi")
	@ResponseBody
	public String  chongzhi(User user,HttpSession session){
		//获得当前用户信息
		String msg=null;
		String unamename=(String)session.getAttribute("unamename");
		User p = service.panduan(unamename);
		//获得用户原有的金额
		double uamount = p.getUamount();
		//将原有的金额与充值的金额相加
		user.setUamount(uamount+user.getJine());
		user.setYinmount(user.getYinmount()-user.getJine());
		int a = service.chongzhi(user);
		if(a>0){
			msg="ok";
			return msg;
		}else{
			msg="no";
			return msg;
		}
	}
}
